package com.jader.activities;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.jader.adapter.SpinnerAdapter;
import com.jader.beans.Address;
import com.jader.myviews.MyProgressDialog;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class AddressUpdateActivity extends BaseActivity implements
		OnClickListener {
	EditText edit_address_receiver, edit_address_phone, edit_address_detail;
	Button btn_address_finish;
	ImageView back;
	TextView actionbar_title;
	String receiver, phone, detail;
	private String url = JaderApplication.getIP() + "/Jader/client";
	private HttpUtils httpUtils;
	Intent intent;
	private String privoiceString, cityString, areaString;
	private Spinner privoice, city, area;
	private SpinnerAdapter privoiceAdapter, cityAdapter, areaAdapter;
	private Address address;
	private MyProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address);
		initView();
		initProvinceDatas();
		receiverData();
	}

	private void receiverData() {
		intent = getIntent();
		address = (Address) intent.getSerializableExtra("address");
		Log.e("address", address+"");
		edit_address_phone.setText(address.getPhone());
		edit_address_detail.setText(address.getDetail());
		edit_address_receiver.setText(address.getReceiver());
	}

	private void initView() {
		edit_address_receiver = (EditText) findViewById(R.id.edit_address_receiver_name);
		edit_address_phone = (EditText) findViewById(R.id.edit_address_receiver_phone);
		edit_address_detail = (EditText) findViewById(R.id.edit_address_receiver_detail);
		btn_address_finish = (Button) findViewById(R.id.btn_address_finish);

		actionbar_title = (TextView) findViewById(R.id.top_actiobar_text);
		actionbar_title.setText("编辑收货地址");
		back = (ImageView) findViewById(R.id.top_actionbar_back);
		btn_address_finish.setOnClickListener(this);
		privoice = (Spinner) findViewById(R.id.province);
		city = (Spinner) findViewById(R.id.city);
		area = (Spinner) findViewById(R.id.area);
	}

	private void initData() {
		privoiceAdapter = new SpinnerAdapter(this, mProvinceDatas);
		privoice.setAdapter(privoiceAdapter);
		privoice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// position为当前省级选中的值的序号
				privoiceString = mProvinceDatas[arg2];
				// 将地级适配器的值改变为city[position]中的值
				cityAdapter = new SpinnerAdapter(AddressUpdateActivity.this,
						mCitisDatasMap.get(privoiceString));
				// 设置二级下拉列表的选项内容适配器
				city.setAdapter(cityAdapter);

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});
		city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				cityString = mCitisDatasMap.get(privoiceString)[arg2];
				areaAdapter = new SpinnerAdapter(AddressUpdateActivity.this,
						mDistrictDatasMap.get(cityString));
				area.setAdapter(areaAdapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		area.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				areaString = mDistrictDatasMap.get(cityString)[arg2];

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	protected void onResume() {
		initData();
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.personale_back:
			finish();
			break;
		case R.id.btn_address_finish:
			checkEdit();
			break;
		default:
			break;
		}

	}

	/**
	 * 更新地址
	 */
	private void updateAddress() {
		int addressid =address.getAddressid();
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "" + JaderApplication.ADDRESS_UPDATE);
		params.addBodyParameter("addressid", addressid+"");
		params.addBodyParameter("receiver", receiver);
		params.addBodyParameter("phoneNum", phone);
		params.addBodyParameter("province", privoiceString);
		params.addBodyParameter("city", cityString);
		params.addBodyParameter("area", areaString);
		params.addBodyParameter("detail", detail);
		httpUtils = new HttpUtils();
		dialog = JaderApplication.getMyDialog(this);
		dialog.show();
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						if(dialog.isShowing()){
							dialog.dismiss();
						}
					
					}

					@Override
					public void onSuccess(ResponseInfo<String> info) {
						String result = info.result;
						if (result.equals("success")) {
							JaderApplication.show(getBaseContext(), "修改成功");
							Intent manager = new Intent(
									AddressUpdateActivity.this,
									AddressManagerActivity.class);
							startActivity(manager);
						} else {
							JaderApplication.show(getBaseContext(), "再试试吧");
						}
						if(dialog.isShowing()){
							dialog.dismiss();
						}
					}
				});
	}

	/**
	 * 检查edit是否为空
	 */
	private void checkEdit() {
		receiver = edit_address_receiver.getText().toString().trim();
		phone = edit_address_phone.getText().toString().trim();
		detail = edit_address_detail.getText().toString().trim();
		if (receiver.equals("")) {
			JaderApplication.show(this, "收货人不能为空");
		} else if (phone.equals("")) {
			JaderApplication.show(this, "联系电话不能为空");
		} else if (detail.equals("")) {
			JaderApplication.show(this, "详细不能为空");
		} else {
			updateAddress();
		}
	}
}
